package edu.hnu.cloudflightserver.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.hnu.cloudflightserver.util.StringUtil;

@WebServlet(name = "DownLoadServlet", urlPatterns = { "/download" })
public class DownLoadServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2716514473283957047L;

	private static String contentType = "application/x-msdownload";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String res = req.getParameter("res");
		if (!StringUtil.isEmpty(res)) {
			String appSrc = req.getServletContext().getRealPath("/WEB-INF/download/" + res);
			File appFile = new File(appSrc);
			if (!appFile.exists()) {
				req.setAttribute("message", "无此资源");
				req.getRequestDispatcher("/WEB-INF/page/message.jsp").forward(req, resp);
			} else {
				downLoad(req, resp, appFile, res);
			}
		} else {
			req.setAttribute("message", "请从正确的下载地址进行下载");
			req.getRequestDispatcher("/WEB-INF/page/message.jsp").forward(req, resp);
		}

	}

	public static void downLoad(HttpServletRequest req, HttpServletResponse resp, File file, String fileName) {
		File appFile = file;
		resp.reset();
		resp.setContentType(contentType);
		resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
		int length = (int) appFile.length();
		resp.setContentLength(length);
		if (length > 0) {

			InputStream ips = null;
			ServletOutputStream sos = null;
			try {
				ips = new FileInputStream(appFile);
				byte[] buff = new byte[1024];
				sos = resp.getOutputStream();
				int readLength;
				while ((readLength = ips.read(buff)) != -1) {
					sos.write(buff, 0, readLength);
				}
				sos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				try {
					req.setAttribute("message", "无此下载资源");
					req.getRequestDispatcher("/WEB-INF/page/message.jsp").forward(req, resp);
				} catch (ServletException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} catch (IOException e) {
				e.printStackTrace();
				try {
					req.setAttribute("message", "发生意外了~请重新下载！");
					req.getRequestDispatcher("/WEB-INF/page/message.jsp").forward(req, resp);
				} catch (ServletException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} finally {
				if (sos != null) {
					try {
						sos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (ips != null) {
					try {
						ips.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}
	}

}
